library(DBI)
library(RSQLite)
library(tidyplots)
library(ggrepel)
source('00_util_scripts/mod_bplot.R')

mydb <- dbConnect(SQLite(), "~/append-ssd/ringtail-test/adgpu.temp3k.db")
mydb

dbListTables(mydb)

temp3k <- dbReadTable(mydb, 'Results') |>
  as_tibble()

dbDisconnect(mydb)

old4.db <- dbConnect(SQLite(), "~/append-ssd/ringtail-test/adgpu.old4.contact.db")

old4 <- dbReadTable(old4.db, 'Results') |>
  as_tibble()

old4 |>
  filter(LigName == 'fpp')

old4is <- dbReadTable(old4.db, 'Interactions') |>
  as_tibble()

old4is |> filter(Pose_ID == 10)

old4ii |>
  filter()

dbDisconnect(old4.db)

best3k <- temp3k |>
  slice_min(docking_score, by = LigName)

best4 <- old4 |>
  slice_min(docking_score, by = LigName) |>
  mutate(LigName = str_to_upper(LigName))

comb3k4 <- best3k |> bind_rows(best4) |>
  mutate(all.rank = rank(docking_score),
         LigName = str_replace(LigName, 'again', 'alt'))

comb3k4 |>
  tidyplot(all.rank, docking_score, width = 150, height = 100) |>
  adjust_font(fontsize = 14) |>
  add_data_points() |>
  add_data_points(data = min_rows(docking_score, 5), color = "red", size = 2) |>
  add_data_points(data = filter_rows(LigName %in% c('FOH', 'FSPP')),
                  color = "red", size = 2) |>
  add_data_labels_repel(data = min_rows(docking_score, 5), LigName,
                        color = 'red', fontsize = 8) |>
  add_data_labels_repel(data = filter_rows(LigName %in% c('FOH', 'FSPP')), LigName,
                        color = 'red', fontsize = 8) +
  labs(title = 'Docking to TRPV3 of 3584 ZINC drug-like molecules',
       y = 'Binding Energy (kCal/mol)')
  
comb3k4 |>
  mutate(all.rank = rank(leff)) |>
  tidyplot(all.rank, leff, width = 150, height = 100) |>
  adjust_font(fontsize = 14) |>
  add_data_points() |>
  add_data_points(data = min_rows(leff, 5), color = "red", size = 2) |>
  add_data_points(data = filter_rows(LigName %in% best4$LigName),
                  color = "red", size = 2) |>
  add_data_labels_repel(data = min_rows(leff, 5), LigName,
                        color = 'red', fontsize = 8) |>
  add_data_labels_repel(data = filter_rows(LigName %in% best4$LigName), LigName,
                        color = 'red', fontsize = 8) +
  labs(title = 'Docking to TRPV3 of 3584 ZINC drug-like molecules',
       y = 'Ligand Efficiency (kCal/mol)')

comb3k4 |>
  tidyplot(docking_score, leff, width = 150, height = 100) |>
  adjust_font(fontsize = 14) |>
  add_data_points() |>
  add_data_points(data = min_rows(docking_score, 5), color = "red", size = 2) |>
  add_data_points(data = filter_rows(LigName %in% c('FOH', 'FSPP')),
                  color = "red", size = 2) |>
  add_data_labels_repel(data = min_rows(docking_score, 5), LigName,
                        color = 'red', fontsize = 8, background = T) |>
  adjust_x_axis(transform = 'reverse') |>
  adjust_y_axis(transform = 'reverse') |>
  add_data_labels_repel(data = filter_rows(LigName %in% c('FOH', 'FSPP')), LigName,
                        color = 'red', fontsize = 8, background = T) +
  labs(title = 'Docking to TRPV3 of 3584 ZINC drug-like molecules',
       x = 'Binding Energy (kCal/mol)', y = 'Ligand Efficiency (kCal/mol)')

# pa-like 4k molecule adgpu ----------
close.4k <- read_csv('trpm7_mucosa/4k.8si6.rt.log', skip = 28, col_names = F)

keymol <- tibble(X1 = c('PA','FOH','NTB'),
       X2 = c(-7.77,-6.94,-9.98),
       X3 = c(-.46,-.29,-.41))

close.4k |> filter(X2 < -7.77)
close.4k |> filter(X3 < -.46)

1331/4525
847/4525
close.4k |>
  ggplot(aes(X2, X3)) +
  geom_point() +
  geom_point(data = keymol, color = 'red') +
  geom_label_repel(data = keymol, aes(label = X1)) +
  theme_pubr() +
  labs(title = 'Virtual screening of 4525 molecules against TRPM7 (8SI6)',
       x = 'Binding Energy (kCal/mol)', y = 'Ligand Efficiency')

# goldilock 461k vs trpv3 ---------
goldi.461k <-
  read_csv('~/append-ssd/ringtail-test/goldi.v3.461k.log', skip = 28,
           col_names = c('id', 'energy', 'lig.eff')) |>
  drop_na(energy)

energy10p <- goldi.461k$energy |> quantile(.05)
leff10p <- goldi.461k$lig.eff |> quantile(.05)

goldi.best3 <- goldi.461k |>
  slice_min(energy, n = 3)

goldi.mark7 <- best4 |>
  mutate(id = LigName, energy = docking_score, lig.eff = leff, .keep = 'none') |>
  bind_rows(goldi.best3) |>
  mutate(id = str_remove_all(id, '\''))

goldi.461k |>
  ggplot(aes(-energy, -lig.eff)) +
  geom_point(size = .2) +
  geom_vline(xintercept = -energy10p, linetype = 'dashed') +
  geom_hline(yintercept = -leff10p, linetype = 'dashed') +
  geom_point(data = goldi.mark7, color = 'red') +
  geom_label_repel(data = goldi.mark7, aes(label = id), alpha = .8,
                   segment.colour = 'red') +
  theme_bw() +
  labs(x = '-Binding energy (kCal/mol)', y = 'Ligand efficiency',
       title = 'ADGPU docking of 461k drug-like molecules vs TRPV3',
       subtitle = 'Drug-like: MW 250-350, hydrophobicity (logp) 1-3')

## leftover ex instock ----------
exins <- read_csv('~/append-ssd/ringtail-test/exinstock.goldi.zincid.txt',
                  col_names = 'id')

1208/399281

goldi.domes <- read_tsv('zinc20.goldi.instoc.3d.uniq.id.uci.tsv.domestic.tsv')

goldi.domes |>
  filter(zinc_id %in% exins$id)

b1 <-
  read_csv('~/append-ssd/ringtail-test/unfini.batch00', col_names = 'path')

exb1 <- b1 |>
  mutate(id = str_extract(path, 'ZINC\\d+')) |>
  filter(id %in% exins$id)

exb1 |>
  select(path) |>
  write_csv('~/append-ssd/ringtail-test/unfini.exbatch00', col_names = F)

## contact analysis ---------
v3.known.sites <- read_tsv('virtual_screen_v3/known.binding.sites.tsv')

v3.known.sites <- v3.known.sites |>
  mutate(rec.aa = str_extract(site, '\\d+'))

#' Title
#'
#' @param db_path 
#' @param interact_aa 
#'
#' @return
#' @export
#'
#' @examples
filter_interaction_ringtail <- function(db_path, interact_aa, id_best = TRUE){
  mydb <-
    dbConnect(SQLite(), db_path)
  
  db.result <- dbReadTable(mydb, 'Results') |>
    as_tibble()
  
  nID.unique <- db.result$LigName |>
    str_extract('ZINC\\d+') |>
    unique() |>
    length()
  
  message(str_glue("Found {nID.unique} unique ZINC ID in total."))
  
  db.int <- dbReadTable(mydb, 'Interaction_indices') |>
    filter(rec_resid %in% interact_aa)
  
  db.pose <- dbReadTable(mydb, 'Interactions') |>
    filter(interaction_id %in% db.int$interaction_id)
  
  dbDisconnect(mydb)
  
  res.int <- db.result |>
    filter(Pose_ID %in% db.pose$Pose_ID) |>
    mutate(zinc_id = str_extract(LigName, 'ZINC\\d+'), docking_score, leff,
           .keep = 'used') |>
    slice_min(docking_score, by = zinc_id)
  
  nID.acted <- res.int$zinc_id |>
    unique() |>
    length()
  
  message(str_glue("Found {nID.acted} unique ZINC ID good in interaction."))
  
  res.int
}

goldi.461k.redo <- '~/append-ssd/ringtail-test/redocked-461k-golid.sqlite'

goldi.461k.final <-
  filter_interaction_ringtail(goldi.461k.redo, v3.known.sites$rec.aa)

### batch1 ----------
b1.top5.path <-
  '~/append-ssd/ringtail-test/unf-goldi-b0-dlg/adgpu.goldi.trpv3.658k1.sqlite_top5.db'

b1.top5.int <-
  filter_interaction_ringtail(b1.top5.path, v3.known.sites$rec.aa)

unf.b1.uci <- read_csv('goldi.unf.b1.p5.int.uci.csv')

unf.b1.chbk <- unf.b1.uci |>
  filter(shortName == 'chemicalbook')

unf.b1.nsrc10 <- unf.b1.uci |>
  mutate(nsrc = n(), .by = zinc_id) |>
  filter(nsrc > 9, !(zinc_id %in% unf.b1.chbk$zinc_id)) |>
  distinct(zinc_id, .keep_all = T)

unf.b1.chbk |>
  bind_rows(unf.b1.nsrc10) |>
  write_csv('virtual_screen_v3/goldi.unf.b1.domestic.csv')

unf.b1.domes <-
  read_csv('virtual_screen_v3/goldi.unf.b1.domestic.csv')

unf.b1.final <- b1.top5.int |>
  slice_min(docking_score, by = zinc_id) |>
  right_join(unf.b1.domes)

### batch2 -----
# only CAS 24769-56-0?
unf.b2.p5 <- '~/append-ssd/ringtail-test/unf-goldi-b1-dlg/unf-golid-b1.sqlite_top5.db'

unf.b2.int <-
  filter_interaction_ringtail(db_path = unf.b2.p5, v3.known.sites$rec.aa)

unf.b2.int |>
  mutate(zinc_id = str_extract(LigName, 'ZINC\\d+')) |>
  distinct(zinc_id) |>
  write_tsv('virtual_screen_v3/goldi.unf.b2.rtp5.int.tsv')

unf.b2.domes <-
  read_tsv('virtual_screen_v3/goldi.unf.b2.domestic.csv.domestic.tsv')

unf.b2.final <- unf.b2.int |>
  right_join(unf.b2.domes)

### batch3 -------
unf.b3.p5 <- '~/append-ssd/ringtail-test/unf-goldi-b2-dlg/unf-golid-b2.sqlite_top5.db'

unf.b3.int <-
  filter_interaction_ringtail(db_path = unf.b3.p5, v3.known.sites$rec.aa)

unf.b3.uci <- read_csv('virtual_screen_v3/goldi.unf.b3.domestic.csv')

unf.b3.final <- unf.b3.uci |>
  mutate(nsrc = n(), .by = zinc_id) |>
  filter(nsrc > 9 | shortName == 'chemicalbook') |>
  distinct(zinc_id, .keep_all = T) |>
  left_join(unf.b3.int)

### batch4 --------
xb3 <-
  read_csv('~/append-ssd/ringtail-test/unfini.exbatch03', col_names = 'path')

xb3 |>
  mutate(zinc_id = str_extract(path, 'ZINC\\d+')) |>
  distinct(zinc_id) |>
  head(60201) |>
  write_tsv('unfini.exbatch03.b2.tsv')

b4.nsrc10 <- read_csv('nsrc10.csv')

b4.cb <- read_csv('chembk.csv')

xb4.domes <- xb3 |>
  mutate(zinc_id = str_extract(path, 'ZINC\\d+')) |>
  filter(zinc_id %in% c(b4.nsrc10$zinc_id, b4.cb$zinc_id))

xb4.domes |>
  distinct(zinc_id)

xb4.domes |>
  select(path) |>
  write_csv('~/append-ssd/ringtail-test/unf.goldi.batch4.domes.pdbqt.lst',
            col_names = F)

unf.b4.p5 <- '~/append-ssd/ringtail-test/unf-golid-b4.sqlite'

unf.b4.final <-
  filter_interaction_ringtail(db_path = unf.b4.p5, v3.known.sites$rec.aa)

## domestic best goldilock -------
ls() |> str_subset('final')

goldi.final <- goldi.461k.final |>
  bind_rows(unf.b1.final, unf.b2.final, unf.b3.final, unf.b4.final) |>
  filter(!is.na(docking_score))

goldi.final |> write_csv('virtual_screen_v3/goldi.int.domes.final.csv')

goldi.final |> slice_min(docking_score, by = zinc_id) |>
  slice_min(docking_score, n = 10) |>
  DT::datatable()

goldi.lig <-
  read_csv('~/append-ssd/ringtail-test/zinc20-instock-goldilock-3d.tmdbox.gpu.txt', col_names = 'path')

goldi.lig |>
  mutate(zinc_id = str_extract(path, 'ZINC\\d+')) |>
  filter(zinc_id %in% cb2$zinc_id) |>
  select(-zinc_id) |>
  write_csv('~/append-ssd/ringtail-test/zinc20-instock-goldilock-3d.chembk.lst', col_names = FALSE)

# fpp-like vs trpv3 ----------
fpplike <-
  read_csv('~/append-ssd/ringtail-test/fpplike.v3.156k.log', skip = 28,
           col_names = c('id', 'energy', 'lig.eff')) |>
  drop_na(energy)

energy10p <- fpplike$energy |> quantile(.05)
leff10p <- fpplike$lig.eff |> quantile(.05)

fppl.best3 <- fpplike |>
  slice_min(energy, n = 3) |>
  mutate(id = str_extract(id, 'ZINC\\d+'))

fppl.mark7 <- best4 |>
  mutate(id = LigName, energy = docking_score, lig.eff = leff, .keep = 'none') |>
  bind_rows(fppl.best3)

fpplike |>
  ggplot(aes(-energy, -lig.eff)) +
  geom_point(size = .2) +
  geom_vline(xintercept = -energy10p, linetype = 'dashed') +
  geom_hline(yintercept = -leff10p, linetype = 'dashed') +
  geom_point(data = fppl.mark7, color = 'red') +
  geom_label_repel(data = fppl.mark7, aes(label = id), alpha = .8,
                   segment.colour = 'red', nudge_x = .1) +
  theme_bw() +
  labs(x = '-Binding energy (kCal/mol)', y = 'Ligand efficiency',
       title = 'ADGPU docking of 156k FPP-like molecules vs TRPV3',
       subtitle = 'FPP-like: MW 375-400, hydrophobicity (logp) 4.5-5')

## contact analysis ---------
v3.known.sites <- read_tsv('virtual_screen_v3/known.binding.sites.tsv')

v3.known.sites <- v3.known.sites |>
  mutate(rec.aa = str_extract(site, '\\d+'))

mydb <-
  dbConnect(SQLite(),
            "~/append-ssd/ringtail-test/fpp-like-trpv3.sqlite_top5.db")
mydb

dbListTables(mydb)

fppl5p <- dbReadTable(mydb, 'Results') |>
  as_tibble()

fppl5p |> colnames()

fppl5p.int <- dbReadTable(mydb, 'Interaction_indices') |>
  as_tibble() |>
  filter(rec_resid %in% v3.known.sites$rec.aa)

fppl5p.pose <- dbReadTable(mydb, 'Interactions') |>
  as_tibble() |>
  filter(interaction_id %in% fppl5p.int$interaction_id)

dbDisconnect(mydb)

## filter for ex-instock -------
fppl.exin <-
  list.files('~/append-ssd/ringtail-test/zinc20-exinstock-fpplike-3d-smi/',
             'smi$', full.names = T) |>
  read_delim()

fppl5p <- fppl5p |>
  filter(Pose_ID %in% fppl5p.pose$Pose_ID) |>
  mutate(zinc_id = str_extract(LigName, 'ZINC\\d+')) |>
  write_csv('virtual_screen_v3/goldi.461k.top5p.knowsites.csv')

fppl5p.rank <- fppl5p |>
  select(zinc_id, docking_score) |>
  summarise(docking_score = min(docking_score), .by = zinc_id) |>
  arrange(docking_score)

fppl5p |>
  ggplot(aes(-docking_score, -leff)) +
  geom_point()

## fpp-like cnn-cnn-ic50 ---------
cnn.cnn.ic <-
  read_csv('~/Documents/projects/DeepPurpose/v3.goldi.cnn-cnn.ic50.dppps.csv')

goldi.tidy <- goldi.461k |>
  mutate(id = str_extract(id, 'ZINC\\d+')) |>
  summarise(energy = min(energy), .by = id)

goldi.comp <- cnn.cnn.ic |>
  filter(Ligand_Name %in% goldi.tidy$id) |>
  select(-SMILES) |>
  summarise(Score = max(Score), .by = Ligand_Name) |>
  left_join(goldi.tidy, by = join_by(Ligand_Name == id))

goldi.comp |>
  ggplot(aes(Score, energy)) +
  geom_point(size = .2) +
  geom_smooth()

cor.test(goldi.comp$Score, goldi.comp$energy)

score95 <- goldi.comp$Score |> quantile(.95)

goldi.dbl.p5 <- goldi.comp |>
  filter(Score >= score95, energy <= energy10p)

goldi.dbl.p5 |> write_csv('virtual_screen_v3/goldi.adgpu.cnncnn.p05.csv')

nrow(goldi.dbl.p5)

nrow(goldi.comp) * .05^2

query_unichem_src <- function(zinc.id) {
  require(httr2)
  req <- request("https://www.ebi.ac.uk/unichem/api/v1/compounds") |>
    req_headers("Accept" = "application/json") |>
    req_body_json(list(compound = zinc.id, sourceID = 9,
                       type = "sourceID"))
  resp <- req_perform(req)
  resp.lst <- resp |> resp_body_json(simplifyVector = T)
  resp.lst$compounds$sources[[1]] |> as_tibble() |>
    select(compoundId, shortName, url)
}

fppl5p.unichem <- fppl5p.rank$zinc_id |>
  unique() |>
  map(safely(query_unichem_src), .progress = T)

named.lst <- fppl5p.unichem |> list_transpose() |>
  pluck('result') |>
  set_names(unique(fppl5p.rank$zinc_id))

fppl5p.unichem <- named.lst |> list_rbind(names_to = 'id')

fppl5p.unichem |> mutate(nsrc = n(), .by = id) |>
  filter(nsrc > 5) |> left_join(fppl5p.rank, join_by(id == zinc_id)) |>
  write_csv('fppl5p.unichem.src6.csv')

fppl5p.rank |>
  mutate(src = fppl5p.unichem, nsrc = nsrc) |>
  filter(nsrc >= 5) |>
  unnest(src) |>
  select(zinc_id, url)

# filter zinc db for domestic --------
zinc20.goldi.instoc.3d <-
  read_delim('~/append-ssd/ringtail-test/z20.instock.gl.3d.smi')

zinc20.goldi.instoc.3d <- zinc20.goldi.instoc.3d |>
  filter(zinc_id != 'zinc_id')

zinc20.goldi.instoc.3d |>
  distinct(zinc_id, smiles)

# agent vs instock -------
goldi.instck <-
read_csv('~/append-ssd/ringtail-test/data/ZINC20-goldilock-instock-3D-smi.uri',
         col_names = 'url')

goldi.agnt <-
  read_csv('~/append-ssd/ringtail-test/data/zinc20-agent-goldilock-3d-smi/ZINC20-goldilock-agent-3D-smi.uri',
           col_names = 'url')

837-711
goldi.agnt |>
  filter(!(url %in% goldi.instck$url)) |>
  write_csv('~/append-ssd/ringtail-test/data/ZINC20-goldilock-exagnt-3D-smi.uri',
            col_names = F)

exagnt <-
list.files('~/append-ssd/ringtail-test/data/zinc20-agent-goldilock-3d-smi/',
           pattern = 'smi$', full.names = T) |>
  read_delim()

exagnt |>
  distinct(zinc_id) |>
  write_tsv('zinc20.goldilock.exagnt.3d.unique.id.tsv')

instoc <- read_tsv('zinc20.goldi.instoc.3d.uniq.id.uci.tsv.domestic.tsv')

exagnt <- read_tsv('zinc20.goldi.exagnt.3d.uniq.id.uci.tsv.domestic.tsv')

bind_rows(instoc, exagnt) |>
  write_tsv('zinc20.goldi.instoc.agnt.3d.domestic.tsv')

## collect pdbqt -----
goldi.instck <-
read_csv('~/append-ssd/ringtail-test/zinc20.instoc.goldi.3d.pdbqt.lst',
         col_names = 'path')

domes.path <- goldi.instck |>
  mutate(zinc_id = str_extract(path, 'ZINC\\d+')) |>
  filter(zinc_id %in% instoc$zinc_id)

domes.path |>
  distinct(zinc_id)
7850

domes.path2 <-
  list.files('~/append-ssd/ringtail-test/zinc20-chembk-goldilock-3d-pdbqt/',
           'pdbqt', full.names = T)

domes.path3 <-
  list.files('~/append-ssd/ringtail-test/zinc20-chembk-goldilock-pdbqt/',
             'pdbqt', full.names = T)

domes.path.got <-
tibble(path = c(domes.path2, domes.path3)) |>
  mutate(zinc_id = str_extract(path, 'ZINC\\d+')) |>
  filter(zinc_id %in% instoc$zinc_id) |>
  bind_rows(domes.path)

domes.path.got |>
  select(path) |>
  write_csv('~/append-ssd/ringtail-test/domes.path.got.lst', col_names = F)

instoc |>
  filter(!(zinc_id %in% domes.path.got$zinc_id)) |>
  bind_rows(exagnt) |>
  mutate(url = str_glue('https://zinc20.docking.org/substances/{zinc_id}.smi'),
         .keep = 'none') |>
  write_csv('~/append-ssd/ringtail-test/insagent8.smi.uri', col_names = F)

# vendor4 vina -----------
mydb <- SQLite() |>
  dbConnect("~/append-ssd/ringtail-test/data/vina_domestic_vendor4.sqlite")

dbListTables(mydb)

v4vina <- dbReadTable(mydb, 'Results') |>
  as_tibble()

v4vina_percent5 <- mydb |>
  dbReadTable('top5') |>
  as_tibble()

dbDisconnect(mydb)

v4vina <- v4vina |>
  slice_min(docking_score, by = LigName)

dscore_p5 <- v4vina$docking_score |> quantile(.05)

leff_p5 <- v4vina$leff |> quantile(.05)

v4vina |>
  ggplot(aes(-docking_score, -leff)) +
  geom_point(size = .2) +
  geom_vline(xintercept = -dscore_p5, linetype = 'dashed') +
  labs(x = '-Binding energy (kCal/mol)', y = 'Ligand efficiency',
       title = 'Vina docking of 15k molecule from vendor catalogs vs TRPV3') +
  theme_bw()

v4vina_percent5 |>
  mutate(LigName = str_extract(LigName, 'ZINC\\d+')) |>
  distinct(LigName)

v4vina_percent5 |>
  ggplot(aes(docking_score, leff)) +
  geom_point()

v4vina_percent5 |>
  slice_min(docking_score, n = 10) |>
  select(LigName)

v4vina_percent5 |>
  mutate(file = str_remove(LigName, '_out') |> str_c('.pdbqt'),
         .keep = 'none') |>
  write_csv('virtual_screen_v3/results/vendor4_vina_best5perc.txt',
            col_names = F)

v4vina_percent5 <-
  read_csv('virtual_screen_v3/results/vendor4_vina_best5perc.txt', col_names = F)

hpcc_done_v4 <-
  list.files('~/append-ssd/ringtail-test/data/zinc20_vendor4_adgpu_out/')

hpcc_done_v4 <-
  tibble(dlg = hpcc_done_v4, prefix = str_remove(dlg, '.dlg'))

v4_adgpu_todo <- v4vina_percent5 |>
  mutate(prefix = str_remove(X1, '.pdbqt')) |>
  filter(!(prefix %in% hpcc_done_v4$prefix))

all_v4 <-
list.files('~/append-ssd/ringtail-test/data/zinc20-vendor4-instock-pdbqt/')

tibble(basename = all_v4, prefix = str_remove(basename, '.pdbqt')) |>
  inner_join(v4_adgpu_todo) |>
  mutate(path = str_c('zinc20-vendor4-instock-pdbqt/', basename), .keep = 'none') |>
  write_csv('~/append-ssd/ringtail-test/data/v4_adgpu_workstat_todo.lst',
            col_names = F)

## adgpu ------------
mydb <- SQLite() |>
  dbConnect("~/append-ssd/ringtail-test/data/adgpu_vendor4_trpv3-tmd.sqlite")

dbListTables(mydb)

v4_adgpu <- dbReadTable(mydb, 'Results') |>
  as_tibble()

dbDisconnect(mydb)

v4_adgpu_d05 <- v4_adgpu$docking_score |> quantile(.05)

v4_adgpu_e05 <- v4_adgpu$leff |> quantile(.05)

v4_adgpu |>
  ggplot(aes(docking_score, leff)) +
  geom_hline(yintercept = v4_adgpu_e05) +
  geom_vline(xintercept = v4_adgpu_d05) +
  geom_point()

v4_adgpu |>
  filter(docking_score < v4_adgpu_d05, leff < v4_adgpu_e05) |>
  write_source_csv('best5_energy_leff_adgpu_trpv3',
                   project = 'virtual_screen_v3')
